package io.github.uicdb.et.recipe;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import io.github.uicdb.et.element.BasicElement;
import net.minecraft.core.Holder;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;

public record ElementRecipePart(Holder<BasicElement> element, int amount) {
    public static final Codec<ElementRecipePart> CODEC = RecordCodecBuilder.create(ins -> ins.group(
            BasicElement.HOLDER_CODEC.fieldOf("element").forGetter(ElementRecipePart::element),
            Codec.INT.fieldOf("amount").forGetter(o -> o.amount)
    ).apply(ins, ElementRecipePart::new));

    public static final StreamCodec<RegistryFriendlyByteBuf, ElementRecipePart> STREAM_CODEC = StreamCodec.composite(
            BasicElement.HOLDER_STREAM_CODEC,
            ElementRecipePart::element,
            ByteBufCodecs.VAR_INT,
            ElementRecipePart::amount,
            ElementRecipePart::new
    );
}
